package com.bear.cmcc.NetTestHTTP.ui.activity;


import com.bear.cmcc.NetTestHTTP.NetworkTestApp;
import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.InfoSubBean;
import com.bear.cmcc.NetTestHTTP.ui.adapter.ChannelsAdapter;
import com.bear.cmcc.NetTestHTTP.ui.view.DragGrid;
import com.bear.cmcc.NetTestHTTP.ui.view.DragGrid.OnMoveItemFinish;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 信息订阅
 * 
 * @author jqa
 * 
 */
public class InfoSubscribleActivity extends AbstractActivity {
	private static final int CHANNELQUEST = 2;
	private LinearLayout linearLayout = null;// 页面指示器
	private DragGrid gridview;
	private ChannelsAdapter adapter;
	private List<InfoSubBean> channels;
	Thread infoThread;
	boolean outofthread;// 判断是否已经退出获取信息线程，如果退出则不提示弹出框，避免界面view出错
	ProgressDialog myprogressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadBottomBar();
		choseBottomItemIndex(2);
		setTitle(R.string.bottom_subscrible_build);
		setContentView(R.layout.activity_info_subscrible);
		//mScrollLayout = (ScrollLayoutViewGroup) findViewById(R.id.ScrollLayoutTest);
		gridview = (DragGrid)findViewById(R.id.infosub_gridview);
		linearLayout = (LinearLayout) findViewById(R.id.pagerindicator);
		NetworkTestApp.loadChannelList(this,false);
		channels = NetworkTestApp.getChannelList();
		init();
	}
	
    @Override
    protected void onStart()
    {
    	choseBottomItemIndex(2);
    	super.onStart();
    }
    
    /**
     * 重载abstract 接口
     * 屏蔽重复点击bottom按钮操作
     * 
     */
    protected void responseForSubscrible()
    {
    	
    }

	private void init() {
		linearLayout.setVisibility(View.GONE);
		adapter = new ChannelsAdapter(this);
		gridview.setAbstractActivity(this);
		gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gridview.setAdapter(adapter);
		
		gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int location,
					long arg3) {
				try{
					InfoSubBean b = adapter.getItem(location);
					if(b.getIsAddChannel()==1){
						//进入管理频道
						startActivityForResult(new Intent(InfoSubscribleActivity.this,
								ChannelMgrActivity.class), CHANNELQUEST);
					}else{
						//进入频道
						Intent intent = new Intent(InfoSubscribleActivity.this, InfoListActivity.class);
						intent.putExtra(InfoListActivity.INFOLIST_ID,  b.getId());
						intent.putExtra(InfoListActivity.INFOLIST_TITLE, b.getTitle());
						startActivity(intent);
					}
				}catch(Exception e){
					
				}
			}
		});
		//每项移动监听
		gridview.setOnMoveItemFinish(new OnMoveItemFinish() {
			
			@Override
			public void moveFinish() {
				NetworkTestApp.loadChannelList(InfoSubscribleActivity.this,true);
				channels = NetworkTestApp.getChannelList();
				if(adapter.getCount()-1<channels.size()){
					//有删除了，则补在后面
					List<InfoSubBean> list = MyCommonUtil.sortInfoSubcrible(channels, adapter.getChannels());
					NetworkTestApp.modifyChannelList(InfoSubscribleActivity.this, list);
					list.clear();
				}else{
					NetworkTestApp.modifyChannelList(InfoSubscribleActivity.this, adapter.getChannels());
				}
				channels.clear();
			}
		});
		
		showWait();
		reload();
		
		//setPagerIndicator();// 设置页面指示器
	}
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			closeWait();
			infoThread = null;

			Object obj = msg.obj;
			if (obj instanceof String) {
				String m = (String) obj;

				if (m == "OK") {
					if(channels!=null){
						for(int i=0;i<channels.size();i++){
							InfoSubBean b = channels.get(i);
							adapter.addObject(b);
						}
					}
					InfoSubBean b = new InfoSubBean();
					b.setId("0");
					b.setIsAddChannel(1);
					adapter.addObject(b);
					adapter.notifyDataSetChanged();
					channels.clear();					
					closeWait();
				}
			} else {
		
			}
		}
	};

	private void postMsg(Object obj) {
		Message msg = Message.obtain();
		msg.obj = obj;
		handler.sendMessage(msg);
	}
	/*
	 * 显示等待提示框 title等待提示标题 content等待提示内容
	 */
	private void showWait() {
		setProgressBarIndeterminateVisibility(true);
		myprogressDialog = InitProgressDialog(myprogressDialog, "", getString(R.string.loadinginfoList),
				this);
		myprogressDialog.show();
	}
	/**
	 * 初始化等待提示框
	 */
	private ProgressDialog InitProgressDialog(ProgressDialog myprogressDialog,
			String title, String message, Activity ac) {
		myprogressDialog = new ProgressDialog(ac);

		myprogressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

		myprogressDialog.setTitle(title);

		myprogressDialog.setMessage(message);

		myprogressDialog.setIndeterminate(false);
		return myprogressDialog;
	}
	/*
	 * 关闭等待提示框
	 */
	private void closeWait() {
		setProgressBarIndeterminateVisibility(false);
		if (myprogressDialog != null) {
			myprogressDialog.cancel();
			myprogressDialog = null;
		}
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case 1:
			if (data != null) {
				boolean modify = data.getBooleanExtra("isModify", false);
				if (modify) {
					adapter.clear();
					adapter.notifyDataSetChanged();
					showWait();
					reload();
				}
			}
			break;

		default:
			break;
		}
	}
	/**
	 * 管理频道后重新载入数据
	 */
	private void reload(){
			infoThread=new Thread(new Runnable() {
				
				@Override
				public void run() {
					NetworkTestApp.loadChannelList(InfoSubscribleActivity.this,false);
					channels = NetworkTestApp.getChannelList();
					postMsg("OK");
				}
			});
			infoThread.start();
	}
	
}
